package com.zyb.warehouse.controller;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.dto.Page;
import com.zyb.warehouse.entity.Purchase;
import com.zyb.warehouse.entity.Store;
import com.zyb.warehouse.service.PurchaseService;
import com.zyb.warehouse.service.StoreService;
import com.zyb.warehouse.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:采购表控制层
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private StoreService storeService;

    /**
     * 添加采购单
     *
     * @param purchase
     * @return
     */
    @PostMapping("/purchase-add")
    public Result addPurchase(@RequestBody Purchase purchase) {
        Result result = purchaseService.addPurchase(purchase);
        return result;
    }

    /**
     * 获取仓库下拉列表
     *
     * @return
     */
    @GetMapping("/store-list")
    public Result getAllStore() {
        List<Store> storeList = storeService.queryAllStore();
        return Result.ok(storeList);
    }

    /**
     * 分页查询采购单
     *
     * @param page
     * @param purchase
     * @return
     */
    @GetMapping("/purchase-page-list")
    public Result page(Page page, Purchase purchase) {
        page = purchaseService.pagePurchase(page, purchase);
        return Result.ok(page);
    }

    /**
     * 删除采购单
     *
     * @param buyId
     * @return
     */
    @DeleteMapping("/purchase-delete/{buyId}")
    public Result delete(@PathVariable("buyId") Integer buyId) {
        Integer i = purchaseService.removePurchase(buyId);
        if (i > 0) {
            return Result.ok("删除成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "删除失败");
    }

    /**
     * 修改采购单
     *
     * @param purchase
     * @return
     */
    @PutMapping("/purchase-update")
    public Result update(@RequestBody Purchase purchase) {
        Result result = purchaseService.modifyPurchase(purchase);
        return result;
    }

    /**
     * 生成入库单
     *
     * @param purchase
     * @return
     */
    @PostMapping("/in-warehouse-record-add")
    public Result addInStore(@RequestBody Purchase purchase, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        Result result = purchaseService.addInStore(purchase, token);
        return result;
    }

}
